/**
 * 布局相关 store
 */
import { defineStore } from "pinia";
import { store } from "@/stores";
import type { setType } from "../types";
import { getConfig } from "@/config";

export const useSettingStore = defineStore({
  id: "sys-setting",
  state: (): setType => ({
    title: getConfig().Title,
    fixedHeader: getConfig().FixedHeader,
    hiddenSideBar: getConfig().HiddenSideBar,
    // TODO: 微前端模式可结合全局环境变量进行优先级判断
    isSubMicro: getConfig().IsSubMicro,
  }),
  getters: {
    getTitle(): PickOne<setType, "title"> {
      return this.title;
    },
    getFixedHeader(): PickOne<setType, "fixedHeader"> {
      return this.fixedHeader;
    },
    getHiddenSideBar(): PickOne<setType, "hiddenSideBar"> {
      return this.hiddenSideBar;
    },
  },
  actions: {
    CHANGE_SETTING({ key, value }) {
      // eslint-disable-next-line no-prototype-builtins
      if (this.hasOwnProperty(key)) {
        this[key] = value;
      }
    },
    changeSetting(data) {
      this.CHANGE_SETTING(data);
    },
  },
});

export function useSettingStoreHook() {
  return useSettingStore(store);
}
